package com.glSto.service.impl;

import com.glSto.service.AuthenticationService;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

@Service
public class AuthenticationServiceImpl implements AuthenticationService {

    @Resource
    private JdbcTemplate jdbcTemplate;

    @Override
    public boolean checkUserExists(String userCode) {
        String sql = "SELECT COUNT(*) FROM PB_User WHERE Code = ? AND Actived = '1'";
        return jdbcTemplate.queryForObject(sql, Integer.class, userCode) > 0;
    }

    @Override
    public Map<String, String> getAuthConfig() {
        String sql = "SELECT * FROM NPMS_SYS_ParamConfig WHERE Title = '统一认证'";
        List<Map<String, Object>> result = jdbcTemplate.queryForList(sql);
        if (result.isEmpty()) {
            return Collections.emptyMap();
        }
        Map<String, Object> row = result.get(0);
        Map<String, String> config = new HashMap<>();
        for (Map.Entry<String, Object> entry : row.entrySet()) {
            config.put(entry.getKey(), entry.getValue() != null ? entry.getValue().toString() : null);
        }
        return config;
    }

    @Override
    public Map<String, String> getOAuthConfig() {
        String sql = "SELECT * FROM PB_OAuthSecret WHERE ROWNUM = 1";
        List<Map<String, Object>> result = jdbcTemplate.queryForList(sql);
        if (result.isEmpty()) {
            return Collections.emptyMap();
        }
        Map<String, Object> row = result.get(0);
        Map<String, String> config = new HashMap<>();
        for (Map.Entry<String, Object> entry : row.entrySet()) {
            config.put(entry.getKey(), entry.getValue() != null ? entry.getValue().toString() : null);
        }
        return config;
    }
}
